package com.adonis.ejoy.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.adonis.ejoy.Item.PopStar;
import com.adonis.ejoy.R;
import com.adonis.ejoy.utils.CircleTransformation;
import com.squareup.picasso.Picasso;

import java.util.List;

public class PopStarAdapter extends RecyclerView.Adapter<PopStarAdapter.ViewHolder>{
    private List<PopStar> mPopStarList;

    static class ViewHolder extends RecyclerView.ViewHolder{
            ImageView PopStarImage;
            TextView PopStarName;
            View PopStarView;

            public ViewHolder(View view){
                super(view);
                PopStarView=view;
                PopStarImage=view.findViewById(R.id.popstar_image);
                PopStarName=view.findViewById(R.id.popstar_name);
            }
    }

    public PopStarAdapter(List<PopStar> popStarList){
        mPopStarList = popStarList;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.popstar_item,parent,false);
        final ViewHolder holder=new ViewHolder(view);

        holder.PopStarView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                int position=holder.getAdapterPosition();
                PopStar popStar=mPopStarList.get(position);
                //这里设置相应的跳转
                Toast.makeText(v.getContext(),"you clicked view "+popStar.getName(),Toast.LENGTH_SHORT).show();
            }
        });
        /*holder.PopStarImage.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                int position =holder.getAdapterPosition();
                PopStar popStar=mPopStarList.get(position);
                //
                Toast.makeText(v.getContext(),"you clicked image"+popStar.getName(),Toast.LENGTH_SHORT).show();
            }
        });*/
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        PopStar popStar = mPopStarList.get(position);
        int avatarSize = holder.itemView.getResources().getDimensionPixelSize(R.dimen.global_menu_avatar_size);
        Picasso.with(holder.itemView.getContext())                     //指定图片的父布局
                .load(popStar.getImageId())
                .resize(avatarSize, avatarSize)                      //设置图片大小
                .centerCrop()                                           //居中裁剪
                .transform(new CircleTransformation())                 //将图片以圆形方式显示
                .into(holder.PopStarImage);
        // holder.PopStarImage.setImageResource(popStar.getImageId());
        holder.PopStarName.setText(popStar.getName());
    }

    @Override
    public int getItemCount() {
        return mPopStarList.size();
    }


}
